package com.tiancai.controller;


import com.tiancai.context.BaseContext;
import com.tiancai.pojo.po.Comment;
import com.tiancai.pojo.po.User;
import com.tiancai.result.Result;
import com.tiancai.service.ICommentService;
import com.tiancai.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tiancai
 * @since 2025-03-11
 */
@RestController
@RequestMapping("/comment")
@Tag(name = "评论管理")
@CrossOrigin
public class CommentController {

    @Autowired
    private ICommentService commentService;

    //新增评论
    @PostMapping("/add")
    @Operation(summary = "新增评论")
    public Result<Comment> add(
            @RequestParam Long workId,
            @RequestParam String content,
            @RequestParam(required = false) Long commentId){
        Comment comment = commentService.addNewComment(workId,content,commentId);
        return Result.success(comment);
    }
    //点赞评论
    @PostMapping("/like")
    @Operation(summary = "点赞评论")
    public Result like(Long commentId){
        commentService.like(commentId);
        return Result.success();
    }

    //根据评论id查找此评论下的所以评论
    @GetMapping("/getCommentByCommentId")
    @Operation(summary = "根据评论id查找此评论下的所以评论")
    public Result<List<Comment>> getCommentByCommentId(Long commentId){
       List<Comment> commentList = commentService.getCommentByCommentId(commentId);
        return Result.success(commentList);
    }

    //删除评论，如果评论下有子评论，则一并删除
    @DeleteMapping("/delete")
    @Operation(summary = "删除评论，如果评论下有子评论，则一并删除")
    public Result delete(Long commentId){
        //删除评论，如果评论下有子评论，则一并删除
        Integer integer = commentService.removeAll(commentId);
        if (integer == 0)
            return Result.error("权限不够，删除失败");
        return Result.success();
    }

}
